*********************************************************************************
*                                                                               *
*            Election Frequency - Preparing Manifesto Project Dataset    	    *
*                                                                               *
*                         		                            					*
*                                                                               *
*********************************************************************************


global beg_path `"C:\Users\JohnSmith\Dropbox\Replication_EF\"'  

use `"${beg_path}Data\Analysis 1\Control Variables\MPDataset_MPDS2019b_stata14.dta"', clear

gen temp1_left = per404 + per405 + per409 + per412 + per413 + per415 + per504 + per701 // Economic planning  Corporatism Keynesian management Controlled economy Nationalisation Marxist analysis Welfare expansion Labour groups 
gen temp1_right = per401 + per402 + per414 + per505 + per702 + per704 // Free enterprise Incentives Economic orthodoxy Welfare limitation Against labour groups Middle class and professional groups
gen economic_axis = temp1_left/(temp1_left+temp1_right)*100
list party if temp1_left ==.
list party if temp1_right ==.
list party if economic_axis ==.	
gen temp2 = 0
replace temp2 = 1 if economic_axis ==. & pervote > 5 	 
egen eco_miss = max(temp2), by(country edate)  // creates an id variable for elections where a party is missing on the eco axis (only parties with vote shares larger than 5 pp) 

*Cultural Axis
gen temp2_left = per103 + per107 + per503 + per602 + per604 + per607 // Anti-imperialism Internationalism Social justice Anti-nationalism Against traditions Multi-culturalism 
gen temp2_right = per109 + per305 + per601 + per603 + per605 + per608 // Anti-internationalism Political authority Nationalism Traditions Law and order Against multi-culturalism
gen cultural_axis = temp2_left/(temp2_left+temp2_right)*100 

gen temp3 = 0
replace temp3 = 1 if cultural_axis ==. & pervote > 5 	 
egen cul_miss = max(temp2), by(country edate)  // creates an id variable for elections where a party is missing on the cultural axis (only parties with vote shares larger than 5 pp) 

gen temp4=0
replace temp4 = 1 if rile ==. & pervote > 5
egen rile_miss = max(temp4), by(country edate)

replace economic_axis = . if eco_miss == 1 
replace cultural_axis = . if cul_miss ==1 
replace rile = . if rile_miss ==1 

collapse (sd) cultural_axis economic_axis rile (iqr) iqrcul=cultural_axis iqreco = economic_axis iqrrile = rile (min) ///
mincul = cultural_axis mineco = economic_axis minrile = rile (max) maxcul = cultural_axis maxeco = economic_axis maxrile = rile [iw=pervote] ///
, by(country date) // weighed

**** Merging variables ****

tostring date, gen(temp1) 
gen Year = substr(temp1, 1, 4)
destring Year, replace


gen Country = . 
replace Country = 	1	if country ==	13	 //	DK
replace Country = 	2	if country ==	33	 //	ES
replace Country = 	3	if country ==	14	 //	FI
replace Country = 	4	if country ==	31	 //	FR
replace Country = 	5	if country ==	34	 //	GR
replace Country = 	6	if country ==	53	 //	IE
replace Country = 	7	if country ==	15	 //	IS
replace Country = 	8	if country ==	54	 //	MT
replace Country = 	9	if country ==	22	 //	NL
replace Country = 	10	if country ==	12	 //	NO
replace Country = 	11	if country ==	35	 //	PT
replace Country = 	12	if country ==	11	 //	SE
replace Country =  	21  if country ==	80	 //	Bulgaria
replace Country =  	22  if country ==	82	 //	Czech Republic 
replace Country =  	23  if country ==	83	 //	Estonia
replace Country =  	24  if country ==	86	 //	Hungary
replace Country =  	25  if country ==	87	 //	Latvia
replace Country =  	26  if country ==	88	 //	Lithuania
replace Country =  	27  if country ==	92	 //	Poland
replace Country =  	28  if country ==	93	 //	Romania
replace Country =  	29  if country ==	96	 //	Slovakia
replace Country =  	30  if country ==	97	 //	Slovenia

sort Country Year date
quietly by Country Year:  gen dup = cond(_N==1,0,_n)

gen legislative = 1 

replace dup = 2 if Country == 4 & Year == 1946  // The coded election is the second one from 1946.

keep if Country !=.
keep Country  Year rile economic_axis cultural_axis dup legislative
save  `"${beg_path}Data\Analysis 1\Control Variables\MP_to_merge.dta"', replace

